package com.social.socialservice.dal;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 多线程定时任务
 */
//@EnableScheduling   // 1.开启定时任务
@EnableAsync        // 2.开启多线程
@Service            // 3.实现层注解
public class MultiThreadScheduleTask {
    @Async
    @Scheduled(fixedDelay = 10000)  //间隔1秒
    public void first() throws InterruptedException {
        System.out.println("第一个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());
        System.out.println("定时任务测试:间隔10秒");
    }

    @Async
    @Scheduled(cron = "0 */1 * * * ?")  //间隔1分钟
    public void second() throws InterruptedException {
        System.out.println("第二个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());
        System.out.println("定时任务测试：间隔1分钟");
    }
}
